﻿// --------------------------------
// <copyright file="MainForm2.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Tronsoft.Actucal.CalculationEngineCreator;
using ICSharpCode.TextEditor.Document;
using Tronsoft.Actucal.CalculationEngineCreator.ScriptConfiguration;

namespace Actucal
{
    public partial class MainForm2 : Form
    {
        private List<Product> products = new List<Product>();
    
        public MainForm2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                ActucalSection section = config.GetSection("ActucalSection") as ActucalSection;
            }
            catch (ConfigurationException ex)
            {
                Debug.WriteLine(ex.ToString());
            }

            if (File.Exists("products.bin"))
            {
                products = Tools.LoadDataAsBinary<List<Product>>("products.bin") ?? new List<Product>();
            }

            txtSourceEditor.TextEditorProperties.ShowSpaces = false;
            txtSourceEditor.TextEditorProperties.ShowTabs = false;
            txtSourceEditor.TextEditorProperties.ShowEOLMarker = false;
            txtSourceEditor.Document.HighlightingStrategy = HighlightingManager.Manager.FindHighlighter("VBNET");

            FillProductsListbox();
        }

        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtProduct.Text))
            {
                MessageBox.Show("Geef een product naam op.");
                return;
            }

            if (products.Exists(delegate(Product product) { return (product.Name == txtProduct.Text); }))
            {
                MessageBox.Show("Het product bestaat al.");
                return;
            }

            products.Add(new Product(txtProduct.Text));
            lbProducts.Items.Add(txtProduct.Text);
        }

        private void btnDeleteProduct_Click(object sender, EventArgs e)
        {
            if (-1 == lbProducts.SelectedIndex)
            {
                MessageBox.Show("Geen product geselecteerd");
                return;
            }

            int index = products.FindIndex(delegate(Product product) { return (product.Name == lbProducts.SelectedItem.ToString()); });
            products.RemoveAt(index);
            FillProductsListbox();
        }

        private void FillProductsListbox()
        {
            lbProducts.Items.Clear();
            foreach (Product product in products)
            {
                lbProducts.Items.Add(product.Name);
            }

            if (lbProducts.Items.Count != 0)
            {
                lbProducts.SelectedIndex = 0;
            }

            if (-1 != lbProducts.SelectedIndex)
            {
                Product currentProduct =
                    products.Find(
                        delegate(Product product) { return (product.Name == lbProducts.SelectedItem.ToString()); });
                FillAttributesListView(currentProduct);
            }
        }

        private void btnAddAttribute_Click(object sender, EventArgs e)
        {
            if (-1 == lbProducts.SelectedIndex)
            {
                MessageBox.Show("Geen product geselecteerd");
                return;
            }

            if (string.IsNullOrEmpty(txtAttributeName.Text))
            {
                MessageBox.Show("Geef een attribuut naam op.");
                return;
            }

            Product currentProduct = products.Find(delegate(Product product) { return (product.Name == lbProducts.SelectedItem.ToString()); });
            currentProduct.AddAttribute(txtAttributeName.Text, txtAttributeWaarde.Text);

            FillAttributesListView(currentProduct);
        }

        private void FillAttributesListView(Product product)
        {
            if (null == product)
            {
                return;
            }

            lvAttributes.Items.Clear();
            foreach (KeyValuePair<string , string> attr in product.Attributes)
            {
                ListViewItem lvi = lvAttributes.Items.Add(attr.Key);
                lvi.SubItems.Add(attr.Value);
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Tools.SaveDataAsBinary<List<Product>>("products.bin", products);
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            if (-1 == lbProducts.SelectedIndex)
            {
                MessageBox.Show("Geen product geselecteerd");
                return;
            }

             Product currentProduct =
                    products.Find(
                        delegate(Product product) { return (product.Name == lbProducts.SelectedItem.ToString()); });

            CalculationModel model = new CalculationModel();
            foreach (KeyValuePair<string , string> attr in currentProduct.Attributes)
            {
                model.AddCalculation(attr.Key, attr.Value);
            }

            txtSourceEditor.Text = model.Source;
        }

        private void lbProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            Product currentProduct =
                    products.Find(
                        delegate(Product product) { return (product.Name == lbProducts.SelectedItem.ToString()); });
            FillAttributesListView(currentProduct);
        }
    }
}
